<?php

namespace app\admin\service\user;

use app\common\model\user\UserVisitLog;
use tmcore\base\BaseService;
use tmcore\exceptions\ValidateException;

/**
 * 浏览日志服务类
 */
class VisitLogService extends BaseService
{
    /**
     * 获取浏览日志列表
     * @return array
     */
    public static function getList(): array
    {
        // 搜索条件
        $searchParam = [
            '%like%' => ['uid','account','url','ip'],
            '=' => ['type'],
            'datetime' => ['create_time'],
        ];
        $where = self::getSearchData($searchParam, [['status', '=', 1], ['module', '=', 'api']]);

        // 返回分页数据
        return UserVisitLog::page($where, self::getPageData(),'id,uid,account,action,url,type,params,ip,create_time', ['id' => 'desc']);
    }

    /**
     * 查看日志详情
     * @param $params
     * @return array
     */
    public static function detail($params) : array
    {
        if(empty($params['id'])) throw new ValidateException('参数异常');
        $row = UserVisitLog::where(['id' => $params['id']])->append(['status_desc'])->hidden(['result','error','trace'])
            ->findOrEmpty();
        if ($row->isEmpty()) return [];
        return $row->toArray();
    }
}